﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Apollo;

namespace Apollo.Website
{
    public partial class Controls_HeaderPings : System.Web.UI.UserControl
    {
        protected const string HEADER_PINGS_VIEWSTATE = "HEADER_PINGS_VIEWSTATE";


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                if (Apollo.Application.CurrentUser != null)
                {
                    IList<PenguinKPI> kpis = Apollo.Application.Meta<PenguinKPI>()
                        .Where<PenguinKPI>(kpi => kpi.User != null && kpi.User.Id == Apollo.Application.CurrentUser.Id)
                        .ToList<PenguinKPI>();

                    foreach (PenguinKPI kpi in kpis)
                    {
                        AddItem(kpi);
                    }
                }
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            PopulatePenguins();

            PopulateDatas();
        }

        protected void PopulatePenguins()
        {
            Header header = ViewState[HEADER_PINGS_VIEWSTATE] as Header;

            if (header != null && !string.IsNullOrEmpty(header.ImageFilePath) && !string.IsNullOrEmpty(header.ImageHoverFilePath))
            {
                LiteralJS.Text = @"
<script type=""text/javascript"">
    var penguins_original = 'url(" + header.ImageFilePath + @")';
    var penguins_big = 'url(" + header.ImageHoverFilePath + @")';
</script>
";
            }
            else
            {
                LiteralJS.Text = @"
<script type=""text/javascript"">
    var penguins_original = '';
    var penguins_big = '';
</script>
";
            }
        }

        protected void PopulateDatas()
        {
            Header header = ViewState[HEADER_PINGS_VIEWSTATE] as Header;

            if (header == null)
                return;

            LiteralTexts.Text = "";
            LiteralValues.Text = "";

            foreach (HeaderItem item in header.Items)
            {
                LiteralTexts.Text += @"<th class=""penguins_text"">" + item.Text + "</th>";
                LiteralValues.Text += @"<td class=""penguins_value"">" + item.Value + "</td>";
            }
        }

        public void AddItem(PenguinKPI kpi)
        {
            // check if the kpi is in a range
            PenguinKPIDefinition penguinRange = Apollo.Application.Meta<PenguinKPIDefinition>()
                .Where(r => r.KPICode == kpi.KPICode && r.KPIStartDoubleValue <= kpi.KPIDoubleValue && r.KPIEndDoubleValue >= kpi.KPIDoubleValue)
                .SingleOrDefault();

            if (penguinRange != null)
                AddItem(new HeaderItem(kpi.KPIName, kpi.KPIValue), penguinRange.ImageFilePath, penguinRange.ImageHoverFilePath);
            else
                AddItem(new HeaderItem(kpi.KPIName, kpi.KPIValue), null, null);
        }

        private void AddItem(HeaderItem item, string image, string imagehover)
        {
            Header header = ViewState[HEADER_PINGS_VIEWSTATE] as Header;

            if (header == null)
            {
                header = new Header() { ImageFilePath = image, ImageHoverFilePath = imagehover };
                header.Items.Add(item);

                ViewState.Add(HEADER_PINGS_VIEWSTATE, header);
            }
            else
            {
                header.Items.Add(item);

                ViewState[HEADER_PINGS_VIEWSTATE] = header;
            }
        }

        [Serializable]
        internal class Header {
            public IList<HeaderItem> Items { get; set; }
            public string ImageFilePath { get; set; }
            public string ImageHoverFilePath { get; set; }

            public Header() {
                this.Items = new List<HeaderItem>();
            }
        }

        [Serializable]
        internal class HeaderItem
        {
            public string Text { get; set; }
            public string Value { get; set; }
            public string CssClass { get; set; }

            public HeaderItem(string text, string value, string cssClass)
            {
                this.Text = text;
                this.Value = value;
                this.CssClass = cssClass;
            }
            public HeaderItem(string text, string value) : this(text, value, null) { }
        }
    }
}